package com.ms.common.util;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.jfinal.plugin.activerecord.Page;

public class MsUtility {
	public static Map makePara(Object obj, String objSearch, Map<String,String[]> paraMap) {
		Map resultMap=new HashMap<>();
        StringBuffer sqlStr = new StringBuffer(128);
        List<Object> values = new ArrayList<Object>();
        Set<String> nameSet = paraMap.keySet();
        try {
            for(String name:nameSet){
                String[] props = name.split("\\.");
                if(props[0].equals(objSearch)){
                    Class<?> type = obj.getClass().getDeclaredField(props[1]).getType();
                    if (paraMap.get(name) != null && !paraMap.get(name)[0].equals("")) {
                        if(type == String.class){
                            sqlStr.append(" and ").append(props[1]).append(" like ? ");
                            values.add("%"+paraMap.get(name)[0]+"%");
                        }else if(type == Integer.class){
                            sqlStr.append(" and ").append(props[1]).append("=?");
                            values.add(paraMap.get(name)[0]);
                        }else if(type == Double.class){
                            sqlStr.append(" and ").append(props[1]).append("=?");
                            values.add(paraMap.get(name)[0]);
                        }else if(type == Boolean.class){
                            sqlStr.append(" and ").append(props[1]).append("= ?");
                            values.add(paraMap.get(name)[0]);
                        }else if(type == Date.class){
                            sqlStr.append(" and ").append(props[1]).append(" = ?");
                            values.add(paraMap.get(name)[0]);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        resultMap.put("sqlStr", sqlStr.toString());
        resultMap.put("paraLst", values.toArray());
        return resultMap;
    }
	
	public static <E> Map<String, Object>  TransToEasyUiPage(Page<E>  page){
		 Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map  
         jsonMap.put("total", page.getTotalRow());//total键 存放总记录数，必须的  
         jsonMap.put("totalCount", page.getTotalRow());
         jsonMap.put("rows", page.getList());//rows键 存放每页记录 list  
         return jsonMap;
	}
	
	public static Long getStartTime(){  
        Calendar todayStart = Calendar.getInstance();  
        todayStart.set(Calendar.HOUR, 0);  
        todayStart.set(Calendar.MINUTE, 0);  
        todayStart.set(Calendar.SECOND, 0);  
        todayStart.set(Calendar.MILLISECOND, 0);  
        return todayStart.getTime().getTime();  
    }  
	
	
}
